package com.bw.home.model.service

import com.bw.common.utils.LogUtils
import com.bw.home.intent.CommentIntent
import com.bw.home.model.api.CommentApi
import com.bw.home.state.CommentState
import com.bw.mvicore.model.BaseModel
import com.bw.net.RetrofitUtils
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.consumeAsFlow

/**
 * @author:created by hahajing
 * 时间：2024/5/6 0006
 * 企鹅号:444511958
 * 描述:
 */
class CommentModel:BaseModel<CommentIntent>() {

    private val api:CommentApi by lazy {
        RetrofitUtils.create(CommentApi::class.java)
    }

    override fun handleIntent() {
        generateViewModelScope {
            intents.consumeAsFlow().collect{
                when(it){
                    is CommentIntent.PublishCommentIntent->{
                        publishComment(it)
                    }
                    is CommentIntent.RemoveCommentIntent->{
                        removeComment(it)
                    }
                    is CommentIntent.GetCommentsIntent->{
                        getComments(it)
                    }
                    is CommentIntent.AgreeCommentIntent->{
                        agreeComment(it)
                    }
                    is CommentIntent.UnAgreeCommentIntent->{
                        unagreeComment(it)
                    }
                }
            }
        }
    }

    private val _state:MutableStateFlow<CommentState> = MutableStateFlow(CommentState.CommentDefaultState)
    val state:MutableStateFlow<CommentState>
        get() = _state

    /**
     * 取消点赞
     */
    private suspend fun unagreeComment(it: CommentIntent.UnAgreeCommentIntent) {
        val result = api.unagreeComment(it.commentid)
        if (result.code==0){
            _state.value = CommentState.CommentCommonSuccess(true)
        }else{
            _state.value = CommentState.CommentFailed(IllegalStateException("取消点赞失败，error:${result.msg}"))
        }
    }

    /**
     * 点赞
     */
    private suspend fun agreeComment(it: CommentIntent.AgreeCommentIntent) {
        val result = api.agreeComment(it.commentid)
        if (result.code==0){
            _state.value = CommentState.CommentCommonSuccess(true)
        }else{
            _state.value = CommentState.CommentFailed(IllegalStateException("点赞失败，error:${result.msg}"))
        }
    }

    /**
     * 获取评论信息
     */
    private suspend fun getComments(it: CommentIntent.GetCommentsIntent) {
        val result = api.getComments(it.datatype,it.itemid)
        if (result.code==0){
            _state.value = CommentState.CommentGetSuccess(result.data)
        }else{
            _state.value = CommentState.CommentFailed(IllegalStateException("获取评论信息失败，error:${result.msg}"))
        }
    }

    /**
     * 删除评论
     */
    private suspend fun removeComment(it: CommentIntent.RemoveCommentIntent) {
        val result = api.removeComment(it.id)
        if (result.code==0){
            _state.value = CommentState.CommentCommonSuccess(true)
        }else{
            _state.value = CommentState.CommentFailed(IllegalStateException("删除评论失败，error:${result.msg}"))
        }
    }

    /**
     * 发布评论
     */
    private suspend fun publishComment(it: CommentIntent.PublishCommentIntent) {
        val result = api.pubishComment(it.entity)
        if (result.code==0){
            _state.value = CommentState.CommentPublishSuccess(result.data)
        }else{
            _state.value = CommentState.CommentFailed(IllegalStateException("发布评论失败，error:${result.msg}"))
        }
    }

    override fun onCleared() {
        super.onCleared()
        LogUtils.d("123","CommentModel onCleared...")
    }
}